﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using WoSoft.PACS.Win.MyToolBars;
using WoSoft.PACS.Win.Forms.SubForms;
using WoSoft.PACS.Data.User;

namespace WoSoft.PACS.Win.Forms
{
    public partial class FrmUsers : FrmBase
    {
        public FrmUsers()
        {
            InitializeComponent();
        }

        public FrmUsers(RibbonBar ribar)
            : base(ribar)
        {
            InitializeComponent();
        }

        public override List<MyToolBars.OperationModel> Operations()
        {
            List<OperationModel> functionInfos = new List<OperationModel>();

            //添加用户
            var addUser = new OperationModel()
            {
                ID = Guid.NewGuid().ToString(),
                Name = "添加",
                Image = global::WoSoft.PACS.Win.Properties.Resources.add2,
                ClickEvent = new EventHandler(addUserClick),
            };
            functionInfos.Add(addUser);

            //编辑
            var editUserButton = new OperationModel()
            {
                ID = Guid.NewGuid().ToString(),
                Name = "编辑",
                Image = global::WoSoft.PACS.Win.Properties.Resources.document_edit,
                ClickEvent = new EventHandler(EditUserClick),
            };
            functionInfos.Add(editUserButton);

            //删除
            var delUserButton = new OperationModel()
            {
                ID = Guid.NewGuid().ToString(),
                Name = "删除",
                Image = global::WoSoft.PACS.Win.Properties.Resources.delete2,
                ClickEvent = new EventHandler(DelUserClick),
            };
            functionInfos.Add(delUserButton);

            //刷新
            var refreshButton = new OperationModel()
            {
                ID = Guid.NewGuid().ToString(),
                Name = "刷新数据",
                Image = global::WoSoft.PACS.Win.Properties.Resources.refresh,
                ClickEvent = new EventHandler(RefreshButtonClick),
            };
            functionInfos.Add(refreshButton);

            return functionInfos;
        }

        //添加用户
        private void addUserClick(object sender, EventArgs e)
        {
            //显示用户
            FrmUserInfo userinfo = new FrmUserInfo();
            userinfo.ShowDialog(this);

            //刷新用户
            LoadUserList();
        }

        //编辑用户
        private void EditUserClick(object sender, EventArgs e)
        {
            if (this.dataGridViewX1.SelectedRows.Count > 0)
            {
                var selectid = this.dataGridViewX1.SelectedRows[0].Cells["colUserID"].Value.ToString();
                FrmUserInfo userinfo = new FrmUserInfo();
                userinfo.AccountID = selectid;
                userinfo.ShowDialog(this);

                //刷新用户
                LoadUserList();
            }
        }

        //删除用户
        private void DelUserClick(object sender, EventArgs e)
        {
            if (this.dataGridViewX1.SelectedRows.Count > 0)
            {
                if (MessageBoxEx.Show("您确认要删除选择的帐户吗?", "删除", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != System.Windows.Forms.DialogResult.OK)
                {
                    return;
                }
                var selectid = this.dataGridViewX1.SelectedRows[0].Cells["colUserID"].Value.ToString();

                AccountData account = new AccountData();
                account.Delete(selectid);
                //刷新用户
                LoadUserList();
            }
            else
            {
                MessageBoxEx.Show("请选择一个需要删除的用户");
            }
        }

        //刷新用户
        private void RefreshButtonClick(object sender, EventArgs e)
        {
            LoadUserList();
        }

        //加载用户到列表中
        private void LoadUserList()
        {
            AccountData account = new AccountData();
            var dt = account.GetAll();
            this.dataGridViewX1.DataSource = dt;

        }

        //加载数据
        private void FrmUsers_Load(object sender, EventArgs e)
        {
            LoadUserList();
        }
    }
}
